package com.example.library.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.library.R;
import com.example.library.myRecyclerview.AttributeDecoration;
import com.example.library.myRecyclerview.CommonAdapter;
import com.example.library.myRecyclerview.base.ViewHolder;
import com.example.library.http.KnowledgeParseEntity.CxCollectionBean.ClassCollectionBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.Nullable;

/**
 * Created by dadong on 2018/8/29.
 * Describe:
 */
public class NewClassicFragment extends Fragment {

    View mView;
    List<ClassCollectionBean> classCollectionBeans = new ArrayList<>();
    RecyclerView rvUsage;
    CommonAdapter<ClassCollectionBean> classicAdapter;
    LinearLayoutManager rvUsageLm;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.view_knowledge_useage, container, false);
        initView(mView);
        return mView;
    }

    private void initView(View view) {
        rvUsage = view.findViewById(R.id.rv_usage);
        rvUsageLm = new LinearLayoutManager(getContext()) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        rvUsageLm.setOrientation(LinearLayoutManager.VERTICAL);
        if (classCollectionBeans.size() > 0) {
            initNewClassicAdapter();
            rvUsage.setAdapter(classicAdapter);
        }
        rvUsage.setLayoutManager(rvUsageLm);
        rvUsage.addItemDecoration(new AttributeDecoration(getContext(), new AttributeDecoration.DecorationCallback() {
            @Override
            public String getGroupId(int position) {
                return classCollectionBeans.get(position).getCxEnglish();
            }

            @Override
            public String getGroupFirstLine(int position) {
                return classCollectionBeans.get(position).getCxEnglish();
            }
        }));
    }

    private void initNewClassicAdapter() {
        classicAdapter = new CommonAdapter<ClassCollectionBean>(getContext(), R.layout.view_example_item, classCollectionBeans) {
            @Override
            protected void convert(ViewHolder holder, ClassCollectionBean classCollectionBean, int position) {
                holder.setText(R.id.tv_english, Html.fromHtml(classCollectionBean.getClassicApp()) + "");
                holder.getView(R.id.tv_chinese).setVisibility(View.GONE);
                holder.getView(R.id.iv_pronunciation).setVisibility(View.GONE);
            }
        };
    }

    public List<ClassCollectionBean> getClassCollectionBeans() {
        return classCollectionBeans;
    }

    public void setClassCollectionBeans(List<ClassCollectionBean> classCollectionBeans) {
        if (this.classCollectionBeans.size() > 0) {
            this.classCollectionBeans.clear();
        }
        this.classCollectionBeans.addAll(classCollectionBeans);
    }
}
